package com.gzsxy.esjy.sign.util;


import com.gzsxy.esjy.sign.SignConstant;
import com.gzsxy.esjy.sign.dto.*;
import com.gzsxy.esjy.sign.enums.SignTypeEnum;
import com.gzsxy.esjy.sign.exception.SignException;
import org.apache.commons.lang3.StringUtils;


/**
 * 签名协议体bean工厂
 * @author zhangxiaolong
 * @since 2022/7/15
 */
public class SignProtocolUtil {

    /**
     * 获取签名设备类型协议体
     * @param signId requestId、任务id
     * @param signType 签名类型 0安卓板 1投屏版
     * @param pdfHttpPath  源PDF地址，http下载地址
     * @param pdfUploadHttp 目录PDF地址，http
     * @param signOkUrl 签字板返回信息接口url（回调接口）
     * @return
     */
    public static BaseSignProtocolDto getSignByType(String signId, int signType, String pdfHttpPath, String pdfUploadHttp, String signOkUrl) {
        if (signType == SignTypeEnum.ANDROID.getType()) {
            return getAndroidSign(pdfHttpPath, pdfUploadHttp, signId);
        } else if (signType == SignTypeEnum.SCREEN.getType()) {
            return getScreenSign(signId, pdfHttpPath, signOkUrl);
        } else {
            throw new SignException("不支持签名类型");
        }
    }



    public static BaseSignProtocolDto getCancelSignByType(String signId, int signType, String signOkUrl) {
        if (signType == SignTypeEnum.ANDROID.getType()) {
            return getAndroidCancelSign(signId);
        } else if (signType == SignTypeEnum.SCREEN.getType()) {
            return getScreenCancelSign(signId, signOkUrl);
        } else {
            throw new SignException("不支持签名类型");
        }
    }

    /**
     * 获取安卓板协议
     *
     * @return 安卓板协议实体
     */
    public static BaseSignProtocolDto getAndroidCancelSign(String signId) {
        return getAndroidSign("", "", SignConstant.CANCEL_SIGN_CMD, signId);
    }

//    /**
//     * 获取安卓板协议
//     *
//     * @param pdfHttpPath   pdf下载地址
//     * @param pdfUploadPath pdf上传地址
//     * @return 安卓板协议实体
//     */
//    public static BaseSignProtocol getAndroidSign(String pdfHttpPath, String pdfUploadPath) {
//        return getAndroidSign(pdfHttpPath, pdfUploadPath, "");
//    }

    /**
     * 获取安卓板协议
     *
     * @param pdfHttpPath   pdf下载地址
     * @param pdfUploadPath pdf上传地址
     * @return 安卓板协议实体
     */
    public static BaseSignProtocolDto getAndroidSign(String pdfHttpPath, String pdfUploadPath, String signId) {
        if (StringUtils.isBlank(pdfHttpPath) || StringUtils.isBlank(pdfUploadPath)) {
            throw new SignException("PDF下载地址 OR 上传地址 不能为空");
        }

        return getAndroidSign(pdfHttpPath, pdfUploadPath, SignConstant.SIGN_CMD, signId);
    }

    /**
     * 获取投屏板签名协议
     *
     * @param signId        业务ID
     * @param pdfHttpPath   pdf下载地址
     * @param httpSignOkUrl 签名回调地址
     * @return 投屏板协议实体
     */
    public static BaseSignProtocolDto getScreenSign(String signId, String pdfHttpPath, String httpSignOkUrl) {
        if (StringUtils.isBlank(pdfHttpPath) || StringUtils.isBlank(httpSignOkUrl)) {
            throw new SignException("PDF下载地址 OR 回调地址 不能为空");
        }
        return getScreenSign(signId, pdfHttpPath, httpSignOkUrl, SignConstant.SIGN_CMD, false);
    }


    /**
     * 获取投屏板取消签名协议
     *
     * @param signId        业务ID
     * @param httpSignOkUrl 签名回调地址
     * @return 投屏板协议实体
     */
    public static BaseSignProtocolDto getScreenCancelSign(String signId, String httpSignOkUrl) {
        return getScreenSign(signId, "", httpSignOkUrl, SignConstant.CANCEL_SIGN_CMD, false);
    }

    /**
     * 获取投屏板协议
     *
     * @param signId        业务ID,发起一次签名相当一次任务
     * @param pdfHttpPath   pdf下载地址
     * @param httpSignOkUrl 签名回调地址
     * @param isSignRecord  是否需要录像
     * @param command       指令
     * @return 投屏板协议实体
     */
    public static BaseSignProtocolDto getScreenSign(String signId, String pdfHttpPath, String httpSignOkUrl, String command, boolean isSignRecord) {
        long millis = System.currentTimeMillis();
        ScreenSignDevMsgDto devMsg = new ScreenSignDevMsgDto(pdfHttpPath, httpSignOkUrl, isSignRecord ? 1 : 0, signId);
        ScreenSignParamDto psMsg = new ScreenSignParamDto(millis + "", signId, devMsg);
        ScreenSignProtocolDtoDto screenSignProtocolDto = new ScreenSignProtocolDtoDto(psMsg, command);

        screenSignProtocolDto.setDevType(SignTypeEnum.SCREEN.getType());
        screenSignProtocolDto.setDevTypeName(SignTypeEnum.SCREEN.getName());
        screenSignProtocolDto.setTaskId(millis + "");
        screenSignProtocolDto.setRequestId(signId);
        screenSignProtocolDto.setDevProtocolHandler(SignConstant.SCREEN);

        return screenSignProtocolDto;
    }

    /**
     * 获取安卓板协议
     *
     * @param pdfHttpPath   pdf下载地址
     * @param pdfUploadPath pdf上传地址
     * @param signId        任务ID
     * @return 安卓板协议实体
     */
    public static BaseSignProtocolDto getAndroidSign(String pdfHttpPath, String pdfUploadPath, String command, String signId) {
        long millis = System.currentTimeMillis();
        AndroidSignParamDto androidSignParamDto = new AndroidSignParamDto(pdfHttpPath, pdfUploadPath);
        AndroidSignMsgDto androidSignMsgDto = new AndroidSignMsgDto(androidSignParamDto, signId);
        AndroidSignProtocolDto androidSignProtocolDto = new AndroidSignProtocolDto(0, androidSignMsgDto);
        androidSignProtocolDto.setDevType(SignTypeEnum.ANDROID.getType());
        androidSignProtocolDto.setDevTypeName(SignTypeEnum.ANDROID.getName());
        androidSignProtocolDto.setTaskId(signId);
        androidSignProtocolDto.setRequestId(signId);
        androidSignProtocolDto.setDevProtocolHandler(SignConstant.ANDROID);
        androidSignProtocolDto.setP_version( millis + "");

        return androidSignProtocolDto;
    }
}
